package com.thx.addressbook.dao;


import com.thx.addressbook.model.AddressbookGroup;
import com.thx.common.orm.PaginationDao;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;



@Repository
public class AddressbookGroupDao extends PaginationDao<AddressbookGroup> {

  private static final Logger logger = LoggerFactory.getLogger(AddressbookGroupDao.class);

  /**
   * 根通信录名称、所属租户id、所属分类id查询分组对象列表.
   * 
   * @param agName 通讯录分组名称,可以为空
   * @param enterpriseId 所属企业租户id,不能为空
   * @param acId 所属分类id,可以为空
   * @return 所属租户id为空，返回空记录集
   */
  public List<AddressbookGroup> findByAgnameAndEnterpriseProperties(String agName,
      Long enterpriseId, Long acId) {
    List<AddressbookGroup> result = null;
    StringBuffer hql = new StringBuffer();
    hql.append(" from AddressbookGroup ");
    if (enterpriseId != null && enterpriseId != 0) {
      List<Object> objs = new ArrayList<Object>();
      this.generateEnterpriseConditions(agName, enterpriseId, acId, hql, objs);
      result = this.findPageList(hql.toString(), objs.toArray());
    }
    return result;
  }

  /**
   * 根通信录名称、所属租户id、所属分类id查询分组对象列表.
   * 
   * @param agName 通讯录分组名称,可以为空
   * @param enterpriseId 所属企业租户id,不能为空
   * @param acId 所属分类id,可以为空
   * @return 所属租户id为空，返回空记录集
   */
  public List<AddressbookGroup> findAllByAgnameAndEnterpriseProperties(String agName,
      Long enterpriseId, Long acId) {
    List<AddressbookGroup> result = null;
    StringBuffer hql = new StringBuffer();
    hql.append(" from AddressbookGroup ");
    if (enterpriseId != null && enterpriseId != 0) {
      List<Object> objs = new ArrayList<Object>();
      this.generateEnterpriseConditions(agName, enterpriseId, acId, hql, objs);
      result = this.createQuery(hql.toString(), objs.toArray()).list();
    }
    return result;
  }


  /**
   * 根据个人id、个人通用分类id、人员所属租户Id查询该用户个人分组分页数据.
   * 
   * @param enterpriseId 所属企业租户id,不能为空
   * @param perAcId 个人通用分类id,可以为空
   * @param personnelId 人员id,不能为空
   * @param agName 分组名称，可以为空
   * @return 所属企业租户id为空，返回空记录集
   */
  public List<AddressbookGroup> findByPersonnelId(Long enterpriseId, Long perAcId, Long personnelId,
      String agName) {
    List<AddressbookGroup> result = null;
    StringBuffer hql = new StringBuffer();
    hql.append(" from AddressbookGroup ");
    if (enterpriseId != null && enterpriseId != 0) {
      List<Object> objs = new ArrayList<Object>();
      this.generatePersonnelConditions(enterpriseId, perAcId, personnelId, agName, hql, objs);
      result = this.findPageList(hql.toString(), objs.toArray());
    }
    return result;
  }

  /**
   * 根据个人id、个人通用分组id、人员所属租户Id查询该用户所有个人分组.
   * 
   * @param enterpriseId 所属企业租户id,不能为空
   * @param perAcId 个人通用分类id,可以为空
   * @param personnelId 人员id,不能为空
   * @param agName 分组名称，可以为空
   * @return 所属企业租户id为空，返回空记录集
   */
  public List<AddressbookGroup> findAllByPersonnelId(Long enterpriseId, Long perAcId,
      Long personnelId, String agName) {
    List<AddressbookGroup> result = null;
    StringBuffer hql = new StringBuffer();
    hql.append(" from AddressbookGroup ");
    if (enterpriseId != null && enterpriseId != 0) {
      List<Object> objs = new ArrayList<Object>();
      this.generatePersonnelConditions(enterpriseId, perAcId, personnelId, agName, hql, objs);
      result = this.createQuery(hql.toString(), objs.toArray()).list();
    }
    return result;
  }

  /**
   * 形成按个人多条件查询的语句.
   * 
   * @param enterpriseId 所属企业租户id,不能为空
   * @param perAcId 个人通用分类id,可以为空
   * @param personnelId 人员id,不能为空
   * @param agName 分组名称，可以为空
   * @param hql 拼装语句
   * @param objs 拼装数据
   */
  private void generatePersonnelConditions(Long enterpriseId, Long perAcId, Long personnelId,
      String agName, StringBuffer hql, List<Object> objs) {
    String xx = "";
    hql.append(" where enterpriseId = ? ");
    objs.add(enterpriseId);
    if (perAcId != null && perAcId != 0) {
      hql.append(" and acId =? ");
      objs.add(perAcId);
    }
    if (StringUtils.isNotBlank(agName)) {
      hql.append(" and agName like ? ");
      objs.add("%" + agName + "%");
    }
    if (personnelId != null && personnelId != 0) {
      hql.append(" and personnelId =? ");
      objs.add(personnelId);
    }
    hql.append(" order by agOrder ");
  }

  /**
   * 形成按企业多条件查询的语句.
   * 
   * @param agName 通讯录分组名称,可以为空
   * @param enterpriseId 所属企业租户id,不能为空
   * @param acId 所属分类id,可以为空
   * @param hql 拼装语句
   * @param objs 拼装数据
   */
  private void generateEnterpriseConditions(String agName, Long enterpriseId, Long acId,
      StringBuffer hql, List<Object> objs) {
    hql.append(" where enterpriseId = ? ");
    objs.add(enterpriseId);
    if (StringUtils.isNotBlank(agName)) {
      hql.append(" and agName like ? ");
      objs.add("%" + agName + "%");
    }
    if (acId != null && acId != 0) {
      hql.append(" and acId =? ");
      objs.add(acId);
    }
    hql.append(" order by agOrder ");
  }


  // select * from addressbook_group g
  // where g.ac_id='5102'
  // and g.enterprise_id = 181
  // and g.personnel_id = 1048

  // /**
  // * 查询通信录组列表页 建立日期 : Apr 18, 2013 <br>
  // * 作者 : 王阿敏<br>
  // *
  // * @param info
  // * @param start
  // * @param limit
  // * @return
  // */
  // @SuppressWarnings("unchecked")
  // public List<AddressbookGroup> queryAllByCondition(Long pId,
  // AddressbookGroup info, final int start,
  // final int limit) {
  // String hql = getHqlPage(pId, info);
  // List<AddressbookGroup> list =
  // createQuery(hql).setFirstResult(start).setMaxResults(limit).list();
  // if (list.size() > 0) {
  // return list;
  // } else {
  // return null;
  // }
  // }
  //
  // /**
  // * 查询通信录组列表页数 建立日期 : Apr 18, 2013 <br>
  // * 作者 : 王阿敏<br>
  // *
  // * @param info
  // * @return
  // */
  // public int queryCount(Long pId, AddressbookGroup info) {
  // String hql = "select count(*) from AddressbookGroup c where 1=1";
  // // String hql = getHql(info);
  // if (info.getAcId() != null) {
  // hql += " and c.acId =" + info.getAcId();
  // Long addressbookId =
  // Long.valueOf(AppContext.getProperties("perAddCategoryId"));
  // if (info.getAcId().equals(addressbookId)) {
  // hql += " and c.personnelId =" + pId;
  // }
  // }
  // if (info.getEnterpriseId() != null) {
  // hql += " and c.enterpriseId =" + info.getEnterpriseId();
  // }
  // if (StringUtils.isNotBlank(info.getAgName())) {
  // hql += " and c.agName like '%" + info.getAgName() + "%'";
  // }
  // String perAddCategoryIdStr =
  // AppContext.getProperties("perAddCategoryId"); // 个人通讯录Id
  // if (info.getAcId().longValue() != Long.valueOf(perAddCategoryIdStr)) {
  // hql += " and c.agId in (select r.parentId from AddressbookRole r where
  // r.enterpriseId ="
  // + info.getEnterpriseId() + " and r.parentType=" +
  // OAConstant.ROLE_Parent_TYPE_GROUP
  // + " and r.objId=" + pId + ") ";
  // }
  // List list = createQuery(hql).list();
  // if (list.size() > 0) {
  // Long count = (Long) list.get(0);
  // return new Integer(count + "");
  // }
  // return 0;
  // }
  //
  // /**
  // * 授权分页 建立日期 : Jun 19, 2013 7:26:37 PM<br>
  // * 作者: 孙耀飞<br>
  // * 模块 : <br>
  // * 描述 : <br>
  // * 1 <br>
  // * 2 <br>
  // */
  // @SuppressWarnings("unchecked")
  // public List<AddressbookGroup> queryAllByConditionRole(Long pId,
  // AddressbookGroup info, final int start,
  // final int limit) {
  // String hql = getHql(pId, info);
  // List<AddressbookGroup> list =
  // createQuery(hql).setFirstResult(start).setMaxResults(limit).list();
  // if (list.size() > 0) {
  // return list;
  // } else {
  // return null;
  // }
  // }
  //
  // public List<AddressbookGroup> queryAllByConditionRole(Long pId,
  // AddressbookGroup info) {
  // String hql = getHql(pId, info);
  // return findPageList(hql);
  // }
  //
  // public List<AddressbookGroup> queryAllByConditionRole(AddressbookGroup
  // info, Long pId) {
  // String hql = getHql(pId, info);
  // List<AddressbookGroup> list = createQuery(hql).list();
  // if (list.size() > 0) {
  // return list;
  // } else {
  // return null;
  // }
  // }
  //
  // /**
  // * 查询通信录组列表页数 建立日期 : Apr 18, 2013 <br>
  // * 作者 : 王阿敏<br>
  // *
  // * @param info
  // * @return
  // */
  // public int queryCountRole(Long pId, AddressbookGroup info) {
  // String hql = "select count(*) from AddressbookGroup c where 1=1";
  // // String hql = getHql(info);
  // if (info.getAcId() != null) {
  // hql += " and c.acId =" + info.getAcId();
  // Long addressbookId =
  // Long.valueOf(AppContext.getProperties("perAddCategoryId"));
  // if (info.getAcId().equals(addressbookId)) {
  // hql += " and c.personnelId =" + pId;
  // }
  // }
  // if (info.getEnterpriseId() != null) {
  // hql += " and c.enterpriseId =" + info.getEnterpriseId();
  // }
  // if (StringUtils.isNotBlank(info.getAgName())) {
  // hql += " and c.agName like '%" + info.getAgName() + "%'";
  // }
  // List list = createQuery(hql).list();
  // if (list.size() > 0) {
  // Long count = (Long) list.get(0);
  // return new Integer(count + "");
  // }
  // return 0;
  // }
  //
  // /**
  // * 查询分类下的通信录分组信息 建立日期 : Apr 18, 2013 <br>
  // * 作者 : 王阿敏<br>
  // *
  // * @param pacid
  // * @return
  // */
  // @SuppressWarnings("unchecked")
  // public List<AddressbookGroup> queryAll(Long personnelId, String pacid,
  // Long enterpriseId) {
  // String perAddCategoryId = AppContext.getProperties("perAddCategoryId");
  // String hql = "";
  // List<AddressbookGroup> list = null;
  // if (pacid.equals(perAddCategoryId)) {
  // hql = "from AddressbookGroup a where a.personnelId = ? and a.acId = ? and
  // a.enterpriseId = ?";
  // hql += " order by agOrder";
  // list = createQuery(hql, new Object[] { personnelId, Long.valueOf(pacid),
  // enterpriseId }).list();
  // } else {
  // hql = "from AddressbookGroup a where a.acId = ? and a.enterpriseId = ?";
  // hql += " order by agOrder";
  // list = createQuery(hql, new Object[] { Long.valueOf(pacid), enterpriseId
  // }).list();
  // }
  //
  // return list;
  //
  // }
  //
  //// /**
  //// * 根据组Id查询分组信息 建立日期 : Apr 18, 2013 <br>
  //// * 作者 : 王阿敏<br>
  //// *
  //// * @param agId
  //// * @return
  //// */
  //// public AddressbookGroup findById(Long agId) {
  //// String hql = "from AddressbookGroup where agId = ? ";
  //// AddressbookGroup addressbookGroup = new AddressbookGroup();
  //// List<AddressbookGroup> list = (List<AddressbookGroup>) createQuery(hql,
  // new Object[] { agId }).list();
  //// if (list.size() > 0) {
  //// addressbookGroup = list.get(0);
  //// }
  //// return addressbookGroup;
  //// }
  //
  //// @SuppressWarnings("unchecked")
  //// public List<AddressbookGroup> findAll() {
  //// String hql = "from AddressbookGroup where 1=1";
  //// List<AddressbookGroup> list = createQuery(hql).list();
  //// return list;
  //// }
  //
  // @SuppressWarnings("unchecked")
  // public List<AddressbookGroup> queryByacId(Long pId, Long acId, Long
  // enterpriseId) {
  // String hql = "";
  // Long addressbookId =
  // Long.valueOf(AppContext.getProperties("perAddCategoryId"));
  // if (acId.equals(addressbookId)) {
  // hql = "from AddressbookGroup g where 1=1 and g.personnelId=? and g.acId=?
  // and g.enterpriseId=? order by agOrder";
  // return createQuery(hql, new Object[] { pId, acId, enterpriseId }).list();
  // } else {
  // hql = "from AddressbookGroup g where 1=1 and g.acId=? and
  // g.enterpriseId=? order by agOrder";
  // return createQuery(hql, new Object[] { acId, enterpriseId }).list();
  // }
  // }
  //
  // /**
  // * 查询通信录分组sql语句 建立日期 : Apr 18, 2013 <br>
  // * 作者 : 王阿敏<br>
  // *
  // * @param info
  // * @return
  // */
  // @SuppressWarnings("unchecked")
  // public String getHql(Long pId, AddressbookGroup info) {
  // String hql = "from AddressbookGroup c where 1=1 ";
  // if (info.getAcId() != null) {
  // hql += " and c.acId =" + info.getAcId();
  // Long addressbookId =
  // Long.valueOf(AppContext.getProperties("perAddCategoryId"));
  // if (info.getAcId().equals(addressbookId)) {
  // hql += " and c.personnelId =" + pId;
  // }
  // }
  // if (info.getEnterpriseId() != null) {
  // hql += " and c.enterpriseId =" + info.getEnterpriseId();
  // }
  //
  // if (StringUtils.isNotBlank(info.getAgName())) {
  // hql += " and c.agName like '%" + info.getAgName() + "%'";
  // }
  // hql += " order by agOrder";
  // return hql;
  // }
  //
  // /**
  // * jiaru 建立日期 : Jun 19, 2013 7:06:49 PM<br>
  // * 作者: 孙耀飞<br>
  // * 模块 : <br>
  // * 描述 : <br>
  // * 1 <br>
  // * 2 <br>
  // */
  // @SuppressWarnings("unchecked")
  // public String getHqlPage(Long pId, AddressbookGroup info) {
  // String hql = "from AddressbookGroup c where 1=1 ";
  // if (info.getAcId() != null) {
  // hql += " and c.acId =" + info.getAcId();
  // Long addressbookId =
  // Long.valueOf(AppContext.getProperties("perAddCategoryId"));
  // if (info.getAcId().equals(addressbookId)) {
  // hql += " and c.personnelId =" + pId;
  // }
  // }
  // if (info.getEnterpriseId() != null) {
  // hql += " and c.enterpriseId =" + info.getEnterpriseId();
  // }
  //
  // if (StringUtils.isNotBlank(info.getAgName())) {
  // hql += " and c.agName like '%" + info.getAgName() + "%'";
  // }
  // String perAddCategoryIdStr =
  // AppContext.getProperties("perAddCategoryId"); // 个人通讯录Id
  // if (info.getAcId().longValue() != Long.valueOf(perAddCategoryIdStr)) {
  // hql += " and c.agId in (select r.parentId from AddressbookRole r where
  // r.enterpriseId ="
  // + info.getEnterpriseId() + " and r.parentType=" +
  // OAConstant.ROLE_Parent_TYPE_GROUP
  // + " and r.objId=" + pId + ") ";
  // }
  // hql += " order by agOrder";
  // return hql;
  // }
  //
  // /**
  // *
  // * @author zhangyong
  // * @date 2013-4-15 下午06:19:22
  // * @param acView
  // * @return
  // */
  // public List<AddressbookGroup> queryAllByCondition(AddressbookGroupView
  // acView) {
  // String hql = this.getHql((AddressbookGroupView) acView);
  // return createQuery(hql).list();
  // }
  //
  // /**
  // *
  // * @author zhangyong
  // * @date 2013-4-15 下午06:19:22
  // * @param acView
  // * @return
  // */
  // public int queryCount(AddressbookGroupView acView) {
  // String hql = "select count(*) " + this.getHql(acView);
  // List list = createQuery(hql).list();
  // if (list.size() > 0) {
  // Long count = (Long) list.get(0);
  // return new Integer(count + "");
  // }
  // return 0;
  // }
  //
  // /**
  // *
  // * @author zhangyong
  // * @date 2013-4-15 下午06:19:22
  // * @param acView
  // * @return
  // */
  // private String getHql(AddressbookGroupView acView) {
  //
  // Long agId = acView.getAgId();
  // Long enterpriseId = acView.getEnterpriseId();
  // Long acId = acView.getAcId();
  // Long personnelId = acView.getPersonnelId();
  //
  // // 组装sql
  // StringBuilder sb = new StringBuilder();
  // sb.append("from AddressbookGroup where 1=1");
  // if (agId != null && agId != 0) {
  // sb.append(" and agId = " + agId);
  // }
  // if (enterpriseId != null && enterpriseId != 0) {
  // sb.append(" and enterpriseId = " + enterpriseId);
  // }
  // if (personnelId != null && personnelId != 0) {
  // sb.append(" and personnelId = " + personnelId);
  // }
  // if (acId != null && acId != 0) {
  // sb.append(" and acId = " + acId);
  // }
  // sb.append(" order by agOrder");
  // return sb.toString();
  // }
  //
  // public void deleteGroupById(Long deptId) {
  // this.delete(deptId);
  // }
  //
  // /**
  // * 获取某个分组的最后一次的更新时间
  // *
  // * @author 冯晋首
  // * @date 2015-6-23 下午2:37:39
  // * @param personnelId
  // * @param acId
  // * @return
  // */
  // public String getGrouplastUpdateTime(Long personnelId, Long acId) {
  // String sql = "select to_char(max(m.maxdate),'yyyy-mm-dd hh24:mi:ss')
  // from("
  // + "select max(t.create_time) maxdate from ADDRESSBOOK_GROUP t where
  // t.personnel_id=? and t.ac_id = ? union all "
  // + "select max(p.update_time) maxdate from ADDRESSBOOK_GROUP p where
  // p.personnel_id=? and p.ac_id =?) m";
  // return (String) createSQLQuery(sql, personnelId, acId, personnelId,
  // acId).uniqueResult();
  // }
}
